package com.mrdeer.merchant.service.impl;

import com.mrdeer.merchant.service.FileService;
import com.mrdeer.common.domain.BusinessException;
import com.mrdeer.common.domain.CommonErrorCode;
import com.mrdeer.common.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author: xie jianchu
 * @create_date: 2020/8/13 0013
 */
@Service
public class FileServiceImpl implements FileService {

    @Value("${oss.qiniu.url}")
    private String url;

    @Value("${oss.qiniu.accessKey}")
    private String accessKey;

    @Value("${oss.qiniu.secretKey}")
    private String secretKey;

    @Value("${oss.qiniu.bucket}")
    private String bucket;

    @Override
    public String upload(byte[] bytes, String fileName) throws BusinessException {
        //调用common下的上传工具类
        try {
            QiniuUtil.update2Qiniu(accessKey, secretKey, bucket, bytes, fileName);
        } catch (RuntimeException e) {
            throw new BusinessException(CommonErrorCode.E_100106);
        }
        //上传成功返回文件的地址(绝对路径)
        return url + fileName;
    }
}
